package com.livrexpress.livrexpressandroid.localdomain;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;

public class Package implements Entity{
	public static final String STATE_PACKAGE_ACCEPTED = "accepter";
	public static final String STATE_PACKAGE_REFUSED = "refuser";
	
	private static String TABLE = "Package";
	private static String COL_ID_CODEBARRE = "idCodeBarre";
	private static int COL_ID_CODEBARRE_NUM = 0;
    private static String COL_SIZE = "size";
    private static int COL_SIZE_NUM = 1;
    private static String COL_WEIGHT = "weight";
    private static int COL_WEIGHT_NUM = 2;
    private static String COL_ID_DELIVERY = "idDelivery";
    private static int COL_ID_DELIVERY_NUM = 3;
    private static String COL_STATE = "state";
    private static int COL_STATE_NUM = 4;
    private static String COL_DECLINE = "decline";
    private static int COL_DECLINE_NUM = 5;
    
    private String idCodeBarre;
    private String size;
    private String weight;
    private String idDelivery;
    private String state;
    private String decline;

	public Package(){
		super();
		this.idCodeBarre = " ";
		this.size = " ";
		this.weight= " ";
		this.idDelivery= " ";
		this.state=" ";
		this.decline=" ";
	}

	public String getIdCodeBarre() {
		return idCodeBarre;
	}

	public void setIdCodeBarre(String idCodeBarre) {
		idCodeBarre=DatabaseUtils.sqlEscapeString(idCodeBarre);
		this.idCodeBarre = idCodeBarre.substring(1, idCodeBarre.length()-1);
	}

	public String getSize() {
		return size;
	}

	public void setSize(String size) {
		size=DatabaseUtils.sqlEscapeString(size);
		this.size = size.substring(1, size.length()-1);
	}

	public String getWeight() {
		return weight;
	}

	public void setWeight(String weight) {
		this.weight = weight;
	}

	public String getIdDelivery() {
		return idDelivery;
	}

	public void setIdDelivery(String idDelivery) {
		this.idDelivery = idDelivery;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}
	
	public String getDecline() {
		return decline;
	}

	public void setDecline(String decline) {
		this.decline = decline;
	}

	@Override
	public void createTable(SQLiteDatabase db) {
		String CREATE_TABLE = "CREATE TABLE " + TABLE + " ("
				+ COL_ID_CODEBARRE + " INTEGER PRIMARY KEY, "
			        + COL_SIZE + " TEXT, "
			        + COL_WEIGHT + " TEXT, "
			        + COL_ID_DELIVERY + " TEXT, "
			        + COL_STATE + " TEXT, "
			        + COL_DECLINE + " TEXT);";
			        db.execSQL(CREATE_TABLE);
	}

	@Override
	public void removeTable(SQLiteDatabase db) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE);
	}

	@Override
	public void insert() {
		ContentValues val = new ContentValues();
		val.put(COL_ID_CODEBARRE, idCodeBarre);
		val.put(COL_SIZE, size);
        val.put(COL_WEIGHT, weight);
        val.put(COL_ID_DELIVERY, idDelivery);
        val.put(COL_STATE, state);
        val.put(COL_DECLINE, decline);
        if(!getPackage()){
        	LivreApplication.getSqliteManager().insert(TABLE, val);
        }
	}

	@Override
	public void update() {
		ContentValues val = new ContentValues();
		val.put(COL_ID_CODEBARRE, idCodeBarre);
		val.put(COL_SIZE, size);
        val.put(COL_WEIGHT, weight);
        val.put(COL_ID_DELIVERY, idDelivery);
        val.put(COL_STATE, state);
        val.put(COL_DECLINE, decline);
        LivreApplication.getSqliteManager().update(TABLE, val, COL_ID_CODEBARRE+" = '"+this.idCodeBarre+"'");
	}
	
	@Override
	public void delete() {
		LivreApplication.getSqliteManager().delete(TABLE, COL_ID_CODEBARRE + " = " +this.idCodeBarre);
		
	}
	public static List<Package> getPackages(String idDelivery)
	{
		Cursor result = LivreApplication.getSqliteManager().rawQuery("SELECT * FROM " + TABLE +" WHERE "+COL_ID_DELIVERY+" = "+idDelivery+";" , null);
		List<Package> packageList = new ArrayList<Package>();
        while(result.moveToNext())
        {
            Package pac = new Package();
            pac.setIdCodeBarre(result.getString(COL_ID_CODEBARRE_NUM));
            pac.setSize(result.getString(COL_SIZE_NUM));
            pac.setWeight(result.getString(COL_WEIGHT_NUM));
            pac.setIdDelivery(result.getString(COL_ID_DELIVERY_NUM));
            pac.setState(result.getString(COL_STATE_NUM));
            pac.setDecline(result.getString(COL_DECLINE_NUM));
            packageList.add(pac);
        }
        
        result.close();
		return packageList;
	}
	public static Package getPackageByCB(String codeBarre){
        Cursor result;
        try
        {
            result = LivreApplication.getSqliteManager().rawQuery("SELECT * FROM " + TABLE + 
                                                                    " WHERE " + COL_ID_CODEBARRE + " = '" + codeBarre +"'" , null);
        }
        catch(Exception e)
        {
            return null;
        }
        if(!result.moveToNext())
        {
            result.close();
            return null;
        }
        Package pac = new Package();
        pac.setIdCodeBarre(result.getString(COL_ID_CODEBARRE_NUM));
        pac.setSize(result.getString(COL_SIZE_NUM));
        pac.setWeight(result.getString(COL_WEIGHT_NUM));
        pac.setIdDelivery(result.getString(COL_ID_DELIVERY_NUM));
        pac.setState(result.getString(COL_STATE_NUM));
        pac.setDecline(result.getString(COL_DECLINE_NUM));
        
        result.close();
        return pac;
		
	}
	private Boolean getPackage(){
        Cursor result = null;
        try
        {
            result = LivreApplication.getSqliteManager().rawQuery("SELECT * FROM "+TABLE+" WHERE "+COL_ID_CODEBARRE+" = '"+this.idCodeBarre+"'" , null);
        }
        catch(Exception e)
        {
            return false;
        }
        if(result.getCount()==0)
        {
            result.close();
           return false;
        }
        else{
            result.close();
    		return true;
        }

	}
}
